import { dateFilter } from "./filter";
/**
 * 获取开始结束时间参数
 * @param {number,string} id = 前几天 本月 上月 本季 本年
 */
export const getDateParams = (id) => {
    let date = new Date();
    let params = {
        endTime: dateFilter(date),
    };
    if (typeof id === "number") {
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        let d = date.getDate();
        if (d - id < 1) {
            m -= 1;
            let dNum = new Date(y, m, 0).getDate();
            d = dNum - Math.abs(d - id);
            if (m < 1) {
                y -= 1;
                m = 12;
            }
        } else {
            d -= id;
        }
        params.startTime = dateFilter(new Date(`${y}-${m}-${d}`));
    } else {
        let Month = date.getMonth();
        let year = date.getFullYear();
        switch (id) {
            case "本月":
                params.startTime = `${year}-${Month + 1}-1`;
                break;
            case "上月":
                if (Month == 0) {
                    year -= 1;
                    Month = 1;
                }
                let a = {
                    startTime: dateFilter(new Date(`${year}-${Month}-${1}`)),
                    endTime: dateFilter(new Date(`${year}-${Month}-${new Date(year, Month + 1, 0).getDate()}`)),
                };
                return a;
                break;
            case "本季":
                let jdArr = [1, 4, 7, 10];
                let jd = Math.trunc((Month + 1) / 3);
                params.startTime = dateFilter(new Date(`${year}-${jdArr[jd]}-1`));

                break;
            case "本年":
                params.startTime = dateFilter(new Date(`${year}-1-1`));
                break;
            default:
                break;
        }
    }
    return params;
};
/**
 * ovse 交叉观察器 用于监听某个节点元素出现在屏幕时执行
 * @param {object} observer 实例对象
 * @param {object} node 节点元素
 * @param {object} params 接口参数
 * @param {function} callback 回调函数
 */
export function ovse(observer, node, params, callback) {
    this[observer] = new IntersectionObserver((entries) => {
        if (entries[0].intersectionRatio <= 0) return;
        this[params] += 20;
        setTimeout(() => {
            callback();
        }, 1000);
        this.$forceUpdate();
        this[observer].unobserve(this[node]);
    });
}
/**
 * 取消触底执行函数的监听
 */
export function offObserve(observer) {
    this[observer].disconnect();
}

let timeout = null;

/**
 * 防抖原理：一定时间内，只有最后一次操作，再过wait毫秒后才执行函数
 *
 * @param {Function} func 要执行的回调函数
 * @param {Number} wait 延时的时间
 * @param {Boolean} immediate 是否立即执行
 * @return null
 */
export function debounce(func, wait = 500, immediate = true) {
    // 清除定时器
    if (timeout !== null) clearTimeout(timeout);
    if (immediate) {
        const callNow = !timeout;
        timeout = setTimeout(() => {
            timeout = null;
        }, wait);
        if (callNow) typeof func === "function" && func();
    } else {
        // 设置定时器，当最后一次操作后，timeout不会再被清除，所以在延时wait毫秒后执行func回调方法
        timeout = setTimeout(() => {
            typeof func === "function" && func();
        }, wait);
    }
}
/**
 * 将svg导出成图片
 * @param node svg节点 => document.querySelector('svg')
 * @param name 生成的图片名称
 * @param width 生成的图片宽度
 * @param height 生成的图片高度
 * @param type 生成的图片类型
 */
export const covertSVG2Image = (node, name, width, height, type = "gif") => {
    let serializer = new XMLSerializer();
    let source = '<?xml version="1.0" standalone="no"?>\r\n' + serializer.serializeToString(node);
    let image = new Image();
    image.src = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(source);
    let canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    let context = canvas.getContext("2d");
    context.fillStyle = "#fff";
    context.fillRect(0, 0, 10000, 10000);
    image.onload = function() {
        context.drawImage(image, 0, 0);
        let a = document.createElement("a");
        a.download = `${name}.${type}`;
        a.href = canvas.toDataURL(`image/${type}`);
        a.click();
    };
};
